package grabber;

import jxl.Workbook;
import jxl.WorkbookSettings;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CarModelsGrabber {

    private static final String BASE_URL = "http://cars.auto.ru";

    public static void main(String[] args) throws IOException, WriteException {
        Map<String, List<String>> cars = grabCars();
        System.out.println("grabbed.");
        carsToExcel(cars);
    }

    private static void carsToExcel(Map<String, List<String>> cars) throws IOException, WriteException {
        File xlsFile = new File("xls/cars.xls");
        WorkbookSettings s = new WorkbookSettings();
        WritableWorkbook workbook = Workbook.createWorkbook(xlsFile, s);
        workbook.createSheet("sheet", 0);
        WritableSheet sheet = workbook.getSheet(0);
        int col = 0;
        int row;
        for (String mark : cars.keySet()) {
            List<String> models = cars.get(mark);
            sheet.addCell(new Label(col, 0, mark));
            row = 1;
            for (String model : models) {
                sheet.addCell(new Label(col, row, model));
                row++;
            }
            col++;
        }
        workbook.write();
        workbook.close();
    }

    private static HashMap<String, List<String>> grabCars() throws IOException {
        HashMap<String, List<String>> cars = new HashMap<String, List<String>>();
        Document doc = Jsoup.connect(BASE_URL).timeout(20000).get();
        Elements marks = doc.select("table.list tr td div.cell-1 a");
        for (Element mark : marks) {
            String markName = mark.text();
            String markUrl = BASE_URL + mark.attr("href");
            doc = Jsoup.connect(markUrl).timeout(20000).get();
            Elements models = doc.select("table tr.group td a");
            List<String> modelsList = new ArrayList<String>();
            for (Element model : models) {
                String modelName = model.text();
                if (!modelsList.contains(modelName) && !modelName.equalsIgnoreCase("все модели") && modelName.length() > 0) {
                    modelsList.add(modelName);
                } else {
                    System.out.println("Ignore:" + modelName);
                }
            }
            models = doc.select("table tr.subgroup td a");
            for (Element model : models) {
                String modelName = model.text();
                if (!modelsList.contains(modelName) && !modelName.equalsIgnoreCase("все модели") && modelName.length() > 0) {
                    modelsList.add(modelName);
                } else {
                    System.out.println("Ignore:" + modelName);
                }
            }
            cars.put(markName, modelsList);
        }
        return cars;
    }
}